/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.h5Data.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.h5Data.domain.H5Data;
import com.fzhucc.modules.h5Data.service.H5DataService;
import com.fzhucc.modules.h5Data.domain.dto.H5DataQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-05-22
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "h5:首页数据管理")
@RequestMapping("/api/h5Data")
public class H5DataController {

    private final H5DataService h5DataService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('h5Data:list')")
    public void exportH5Data(HttpServletResponse response, H5DataQueryCriteria criteria) throws IOException {
        h5DataService.download(h5DataService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询h5首页数据")
    @ApiOperation("查询h5首页数据")
    @PreAuthorize("@el.check('h5Data:list')")
    public ResponseEntity<Object> queryH5Data(H5DataQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(h5DataService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增h5首页数据")
    @ApiOperation("新增h5首页数据")
    @PreAuthorize("@el.check('h5Data:add')")
    public ResponseEntity<Object> createH5Data(@Validated @RequestBody H5Data resources){
        return new ResponseEntity<>(h5DataService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改h5首页数据")
    @ApiOperation("修改h5首页数据")
    @PreAuthorize("@el.check('h5Data:edit')")
    public ResponseEntity<Object> updateH5Data(@Validated @RequestBody H5Data resources){
        h5DataService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除h5首页数据")
    @ApiOperation("删除h5首页数据")
    @PreAuthorize("@el.check('h5Data:del')")
    public ResponseEntity<Object> deleteH5Data(@RequestBody Integer[] ids) {
        h5DataService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/showH5Data")
    @Log("查询h5首页数据4个")
    @ApiOperation("查询h5首页数据4个")
    public ResponseEntity<Object> showH5Data(){
        return new ResponseEntity<>(h5DataService.showH5Data(), HttpStatus.OK);
    }
}